using Engine;
using Game;

namespace RuthlessConquest {
    public class CountdownDialog : Dialog {
        public Client Client;
        public LabelWidget MessageWidget;
        public int LastCounter = -1;
        public bool IsHiding;
        public const string fName = "RCCountdownDialog";

        public CountdownDialog(Client client) {
            Client = client;
            Children.Add(new InterlaceWidget());
            Children.Add(
                MessageWidget = new LabelWidget { HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center }
            );
        }

        public override void Update() {
            if (!Client.IsConnected) {
                DialogsManager.HideDialog(this);
            }
            else {
                if (!Client.Game.StepModule.IsGameStarted
                    || !(Client.Game.CreationParameters.CountdownTicksCount * 0.5f - Client.Game.StepModule.CountdownStepsLeft * (1f / 60f) > 4f)) {
                    return;
                }
                int num = (int)MathUtils.Ceiling(Client.Game.StepModule.CountdownStepsLeft * (1f / 60f));
                if (num > 0) {
                    if (LastCounter < 0) {
                        LastCounter = num;
                    }
                    if (num != LastCounter) {
                        MessageWidget.Text = string.Format(LanguageControl.Get(fName, "1"), num);
                        MessageWidget.FontScale = 1f;
                        LastCounter = num;
                        AudioManager.PlaySound(Sounds.Countdown, false);
                    }
                }
                else if (!IsHiding) {
                    IsHiding = true;
                    AudioManager.PlaySound(Sounds.Start, false);
                    MessageWidget.Text = LanguageControl.Get(fName, "2");
                    MessageWidget.FontScale = 1.5f;
                    Time.QueueTimeDelayedExecution(Time.FrameStartTime + 0.6, delegate { DialogsManager.HideDialog(this); });
                }
            }
        }
    }
}